package com.cairenhui.trust.auto.task.executor;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cairenhui.trust.auto.task.dao.CrmOrderInfoVMapper;
import com.cairenhui.trust.auto.task.dao.VideoWitnessOrderMapper;
import com.cairenhui.trust.auto.task.model.CrmOrderInfoV;
import com.cairenhui.trust.auto.task.model.CrmOrderInfoVExample;
import com.cairenhui.trust.auto.task.model.CrmOrderInfoVExample.Criteria;
import com.cairenhui.trust.auto.task.model.Page;
import com.cairenhui.trust.auto.task.runnable.WitnessOrderFetchRunnable;
import com.cairenhui.trust.auto.task.util.DateUtils;

/**
 * 
 * @author 黄瑞庆 TODO 合同更新（订单更新） 2017年2月27日
 */
@Component
public class WitnessOrderFetchExecutor extends EasyAutotaskExecutor {

	/** self4j */
	private Logger logger = LoggerFactory
			.getLogger(WitnessOrderFetchExecutor.class);

	/**
	 * 执行数
	 */
	private static Integer round = 200;
	/**
	 * 最大线程数
	 */
	private static Integer maxThread = 100;
	/**
	 * 总条数
	 */
	private static Integer TOATA_COUNT = 0;
	@Autowired
	private VideoWitnessOrderMapper videoWitnessOrderMapper;
	@Autowired
	private CrmOrderInfoVMapper crmOrderInfoVMapper;

	@Override
	public String getCron() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean inuse() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void execute() {

		logger.info(
				this.getClass().getSimpleName() + "更新订单信息开始>>>>>>开始时间{}",
				DateUtils.getStrDate(new Date(), DateUtils.STRDATE_1
						+ DateUtils.HOURS_MINUTES_SECONDS));
		Date start = new Date();

		// 1 设置同步数据参数
		// 最后同步时间
		Date lastDate = videoWitnessOrderMapper.getLasterSyncTime();
		Timestamp lastUpdateTime = null;
		if (lastDate != null) {
			lastUpdateTime = new Timestamp(lastDate.getTime());
		}
		Timestamp nowUpdateTime = new Timestamp(System.currentTimeMillis());

		// 获取总条数
		CrmOrderInfoVExample crmOrderInfoVExample = new CrmOrderInfoVExample();
		Criteria criteria =   crmOrderInfoVExample.createCriteria();
		criteria.andReservationDateLessThanOrEqualTo(nowUpdateTime);
		if (lastUpdateTime != null) {
			criteria.andReservationDateGreaterThanOrEqualTo(lastUpdateTime);
		    crmOrderInfoVExample.or().andModifyDateGreaterThanOrEqualTo(lastUpdateTime);
		}
		TOATA_COUNT = crmOrderInfoVMapper.countByExample(crmOrderInfoVExample);

		// 使用线程
		// 1、计算开启线程数
		Page<CrmOrderInfoV> pageTemp = new Page<CrmOrderInfoV>(1, round,
				TOATA_COUNT);

		for (int i = 1; i <= pageTemp.getTotalPage(); i++) {

			Page<CrmOrderInfoV> page = new Page<CrmOrderInfoV>(i, round,
					TOATA_COUNT);
			crmOrderInfoVExample.setPage(page);
			List<CrmOrderInfoV> listCrmOrderInfoV = crmOrderInfoVMapper
					.selectByExample(crmOrderInfoVExample);
			// 开启线程
			if (listCrmOrderInfoV.size() > 0) {
				new WitnessOrderFetchRunnable(listCrmOrderInfoV).run();
				try {
					Thread.sleep(1000 * 3);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

		}
		Date end = new Date();
		logger.info(
				this.getClass().getSimpleName() + "更新订单信息结束>>>>>>结束时间{},耗时{}秒",
				DateUtils.getStrDate(new Date(), DateUtils.STRDATE_1
						+ DateUtils.HOURS_MINUTES_SECONDS),
				DateUtils.getTimeDifference(start, end));
	}

	public static Integer getRound() {
		return round;
	}

	public static void setRound(Integer round) {
		WitnessOrderFetchExecutor.round = round;
	}

	public static Integer getMaxThread() {
		return maxThread;
	}

	public static void setMaxThread(Integer maxThread) {
		WitnessOrderFetchExecutor.maxThread = maxThread;
	}

}
